//
//  DayReportListTableViewController.swift
//  BellManage
//
//  Created by Bestsch on 16/4/28.
//  Copyright © 2016年 wsl. All rights reserved.
//

import UIKit

class DayReportListTableViewController: BaseViewController,UITableViewDelegate,UITableViewDataSource {

    var tableView: UITableView!
    
    
    var dataSource = NSMutableArray()
    var page = 1
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        self.title = "数据日报"
    }

    func InitViews(){
        
        tableView = UITableView.init(frame: CGRect(x: 0, y: 0, width: screenW, height: screenH))
        tableView.separatorStyle = UITableViewCellSeparatorStyle.none
        tableView.delegate = self
        tableView.dataSource = self
        tableView.tableFooterView = UIView()
        self.view.addSubview(tableView)
        
        setUpRefresh()
    }
    /**
     设置刷新，加载
     */
    func setUpRefresh() {
        
        tableView.header = MJRefreshNormalHeader.init(refreshingTarget: self, refreshingAction: #selector(DayReportListTableViewController.refresh))
        tableView.footer = MJRefreshAutoNormalFooter.init(refreshingTarget: self, refreshingAction: #selector(DayReportListTableViewController.loadMore))
        tableView.header.beginRefreshing()
    }
    /**
     刷新
     */
    func refresh() {
        page = 1
        let user = getUserPojo()
        let url = server + adminwebapppi
        let request = "\(requestHeader)27</t><sch>\(user.schid)</sch><top>5</top><pageindex>\(page)</pageindex></channel></rss>"
        dataSource = getData(url, request: request)
        
        tableView.reloadData()
        tableView.header.beginRefreshing()
    }
    /**
     加载更多
     */
    func loadMore() {
        page += 1
        
        let user = getUserPojo()
        let url = server + adminwebapppi
        let request = "\(requestHeader)27</t><sch>\(user.schid)</sch><top>5</top><pageindex>\(page)</pageindex></channel></rss>"
        dataSource.addObjects(from: getData(url, request: request) as [AnyObject])
        
        tableView.reloadData()
        tableView.header.beginRefreshing()
        
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataSource.count
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 100
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let identifier = "DayReportListTableViewCellIdentifier"
        let dic = dataSource[indexPath.row] as! JSON
        let cell: DayReportListTableViewCell = DayReportListTableViewCell.init(dictionary: dic, reuseIdentifier: identifier)
        return cell
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
